import Vue from 'vue'
import Router from 'vue-router'
import Login from '@/views/Login.vue'
import Layout from '@/layout/index'
import WujieChild from '@/components/wujie/WujieChild.vue'
Vue.use(Router)

// 固定路由
const constantRoutes = [
  {
    path: '',
    name: 'Home',
    component: Layout,
    meta: { title: '首页目录', show: true },
    children: [
      {
        path: 'home',
        component: () => import('@/views/Home.vue'),
        meta: { title: '首页', show: true },
      },
    ],
  },
  {
    path: '/system',
    component: Layout,
    meta: { title: '系统管理', show: true },
    redirect: '/system/userList',
    children: [
      {
        path: 'userList',
        name: 'UserList',
        component: () => import('@/views/system/user/UserList.vue'),
        meta: { title: '用户管理', show: true },
      },
      {
        path: 'roleList',
        name: 'RoleList',
        component: () => import('@/views/system/role/RoleList.vue'),
        meta: { title: '角色管理', show: true },
      },
      {
        path: 'menuList',
        name: 'MenuList',
        component: () => import('@/views/system/menu/MenuList.vue'),
        meta: { title: '菜单管理', show: true },
      },
    ],
  },
  {
    path: '/login',
    name: 'Login',
    component: Login,
  },
  // 无界微前端容器
  {
    path: '/sub-vue2-app/*',
    component: Layout,
    meta: { title: '微前端', show: false },
    children: [
      {
        path: '',
        component: WujieChild,
        meta: { title: 'vue2子应用', show: true },
        props: { defaultPath: '/home' }, // 默认加载子应用的首页
      },
    ],
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: () => import('@/views/other/404.vue'),
  },
]

const router = new Router({
  mode: 'history',
  scrollBehavior: () => {
    return { y: 0 }
  },
  routes: constantRoutes,
})

router.beforeEach((to, from, next) => {
  console.log('to', to.path)
  console.log('from', from.path)

  if (to.path === from.path && to.name === from.name) {
    return next(false)
  }
  next()
})
export default router
